package commons.utils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import commons.params_appli.ParamsAppli;
import commons.errors.ErrorsHandler;
import java.net.URL;
import java.net.URLDecoder;
import java.nio.charset.Charset;
import java.util.logging.Level;
import java.util.logging.Logger;

public class UtilsFile {

	static public ArrayList<String> readFile (String fullPath) {
		if (fullPath==null) return null;
		try {
			// Lecture du fichier
			InputStream is = new FileInputStream(fullPath);

			InputStreamReader isr = new InputStreamReader(is, ParamsAppli.getParam("common.CHARSET_FICHIER_ECHANGE"));
			BufferedReader br = new BufferedReader(isr);
			String ligne;
			ArrayList<String> vLigne = new ArrayList<String>(30000);
			
			while ((ligne = br.readLine()) != null) {
				vLigne.add (ligne);
			}

			br.close();
			isr.close();
			is.close();
			return vLigne;

		} catch (FileNotFoundException ex) {
			ErrorsHandler.addError("Fichier introuvable :\n -> " + fullPath);
			return null;
		} catch (IOException ex) {
			ErrorsHandler.addError("Erreur I/O lors de la lecture du fichier : " + fullPath);
			return null;
		}
	}

	static public boolean writeFile (String path, String fileName, ArrayList<String> vLines) {
		return writeFile(path + "\\" + fileName, vLines);
	}

	static public boolean writeFile (String fullPath, ArrayList<String> vLines) {

		try {
			new File(fullPath).getParentFile().mkdirs();
			new File(fullPath).createNewFile();
		
			OutputStream os = new FileOutputStream(fullPath);
			OutputStreamWriter isw = new OutputStreamWriter(os, ParamsAppli.getParam("common.CHARSET_FICHIER_ECHANGE"));
			BufferedWriter bw = new BufferedWriter(isw);
			String line;

			int nbLine = vLines.size();
			for (int i = 0; i <nbLine ; i++) {
				if (i>0) bw.newLine();
				line = vLines.get(i);

				bw.write(line);
			}

			bw.close();
			return true;

		} catch (IOException ex) {
			//ex.printStackTrace();
			ErrorsHandler.addError("Erreur I/O lors de la création ou lors de l'écriture dans le fichier : " + fullPath);
			return false;
		}
	}

	static public boolean fileExists(String path) {
		return new File(path).exists();
	}

	static public boolean isFileNameValide (String fileName) {
		String[] ILLEGAL_CHARACTERS = { "/", "\n", "\r", "\t", "\0", "\f", "'", "?", "*", "\\", "<", ">", "|", "\"", ":" };

		if (fileName==null || fileName.length()==0)
			return false;
		
		for (String c : ILLEGAL_CHARACTERS)
			if (fileName.indexOf(c) != -1)
				return false;
		return true;
	}

	// On autorise n caractère normaux (pas de ponctuation ni caractère ésotériques ...)
	static public boolean isExtensionValide (String ext) {
		return ext.matches("\\p{Alpha}+");
	}

	static public boolean isSubdirectoryNameValide (String subDirectoryName) {
		return isFileNameValide(subDirectoryName);
	}

	// idem , mais on autorise ':' et '\' (sauf a la fin)
	// et on oblige à avoir un nom de lecteur au debut
	static public boolean isAbsoluteDirectoryNameValide (String directoryName) {
		String[] ILLEGAL_CHARACTERS = { "/", "\n", "\r", "\t", "\0", "\f", "'", "?", "*", "<", ">", "|", "\"" };

		if (directoryName==null || directoryName.length()==0)
			return false;

		if (!directoryName.matches("\\p{Alpha}:\\\\.*"))
			return false;

		if (directoryName.endsWith("\\"))
			return false;

		for (String c : ILLEGAL_CHARACTERS)
			if (directoryName.indexOf(c) != -1)
				return false;
		return true;
	}

	static public boolean isValideCharSet (String charSetName) {
		return Charset.isSupported(charSetName);
	}

}
